Hi guys, I'm new here and also new to C programming. So i got an assignment on C about encrypt and decrypt the string input. I need to use all what I've learn so far which the basic, array, function, pointer, string. I've already learn file processing but I'm not sure if I can include it in this assignment.
Basically, user need to input a string so that the program will decrypt it into other character(in my case I'm encrypting the string into alphanumeric char so that it will be easy to read by user.). Then the program will decrypt the encrypted string back to it's origin.
Here's how the flow should works :
Input (user input string) => gh6T0 (encrypted string)=> Input (decrypt to original string)
Here I've write some code but it's a little bit messy but hope you guys can read it. The problem I faced here's that I can't decrypt the value back to origin since I'm using modulo in the encrypted part (modulo is irreversible calculation), so that means impossible for me to decrypt back to origin string. Anyone please help me overcome this please :'( Thanks in advance, and pardon for my bad english.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 10 //string max size
void encrypt(char [], int []); //function prototype for encrypt
void decrypt(char [], int []); //function prototype for decrypt
int main() //main function/body
{
char password[SIZE]; //input array
int randNO[SIZE]; //random number array to be used in both enc&decrypt function
int j=0, count;
srand(time(NULL));
puts("Enter your password: ");
gets(password); //get input string from user
count=strlen(password);
for(j=0;j<count;j++)
randNO[j]=10+rand()%(10000-10+1); //generate random number to be used in enc&decrypt function
encrypt(password, randNO); //encrypt function call
decrypt(password, randNO); //decrypt function call
system("pause");
return 0;
}
void encrypt(char *PASSWORD, int *randNO) //encrypt function body
{
int i=0, j=0, k=0;
int count=0;
char alphanum[] ="0123456789""abcdefghijklmnopqrstuvwxyz""ABCDEFGHIJKLMNOPQRSTUVWXYZ";
count=strlen(PASSWORD);
for(j=0;j<count;j++)
randNO[j]=10+rand()%(10000-10+1);
printf("Your password is : ");
puts(PASSWORD);
printf("Encryption in progress...\n");
for(i=0;i<count;i++)
{
PASSWORD[i] = alphanum[randNO[i]%(sizeof(alphanum) - 1)]; //encrypting
}
PASSWORD[count]='\0';
puts(PASSWORD); //encrypted value will be printed here
return ;
}
void decrypt(char *PASSWORD, int *randNO) //decrypt function body
{
int i=0, j=0, k=0;
int count=0;
char alphanum[] ="0123456789""abcdefghijklmnopqrstuvwxyz""ABCDEFGHIJKLMNOPQRSTUVWXYZ";
count=strlen(PASSWORD);
printf("Your encrypted password is : ");
puts(PASSWORD);
printf("Decryption in progress...\n");
for(i=0;i<count;i++)
{
PASSWORD[i] = alphanum[randNO[i]%(sizeof(alphanum) + 1)];
}
PASSWORD[count]='\0';
puts(PASSWORD);
return ;
}